package package1;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class TestForCurrentFolderPack {

	/**
	 * put the bat into a folder and it packs everything into zip after double
	 * clicking it
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		
		//for user
		//1. put the install file into destination folder
		//2. double click to install by creating jar file in appointed location and shortcut in desktop and meanwhile delete the install file
		//3. trying start the shortcut to test if the destination folder has been packed on desktop
		
		//for programmer
		//1. 
		//1. packing function with parameters
//					 packing function packed in jar and put in jar location
		//2. 
		//3. 
		
		// create a jar in a local folder and use bat file to call this jar file
		// to work(pack them up)

		StringBuffer sb = new StringBuffer();
		File folder = new File(getCurrentFolder());
		folder = new File(getDesktop() + "\\test");
		for (File f : folder.listFiles()) {
			sb.append(convertCharset(f.getName(), getSystemEncodingCharset(), "utf-8") + "\r\n");
		}
		System.err.println("filenames:" + sb.toString());
		writeToFile(folder.getAbsolutePath() + "\\manifest.mf", sb.toString());
		pack(folder);
	}

	@SuppressWarnings("unused")
	private static void open(File b) throws IOException {
		open(b.getAbsolutePath());
	}

	@SuppressWarnings("unused")
	private static void pack(String s) throws Exception {
		pack(new File(s));
	}

	private static void pack(File f) throws Exception {

		String env = System.getProperty("user.dir").toLowerCase();

		if (env.contains("desktop")) {
			System.err.println("env is desktop");
			run("jar vcf " + f.getName() + ".zip " + f.getName());
		} else if (env.contains("workspace")) {
			System.err.println("env is workspace");
			File temp = new File(getCurrentFolder() + "\\" + f.getName());
			System.err.println(temp.getAbsolutePath());
			if (!temp.exists())
				temp.mkdirs();
			// openCurrentFolder();
		} else
			throw new Exception("first see such env:" + env);

	}

	private static void checkIsFolder(File file) throws Exception {
		if (!file.isDirectory())
			throw new Exception(file.getName() + " is not a directory");
	}

	/**
	 * f1:file, f2:folder
	 */
	@SuppressWarnings("unused")
	private static void copyFile(File f1, File f2) throws Exception {
		checkIsFolder(f2);
		File f = new File(f2.getParent(), f1.getName());
		copy(f1, f);
	}

	private static void copy(File f1, File f2) throws IOException {
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f1));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f2));
		byte[] b = new byte[1024];
		int pos;
		while ((pos = bis.read(b)) != -1) {
			bos.write(b, 0, pos);
			bos.flush();
		}
		bis.close();
		bos.close();
	}

	private static void run(String cmd) throws IOException {
		System.err.println(cmd);
		Process p = Runtime.getRuntime().exec(cmd);
		InputStream is1 = p.getInputStream();
		InputStream is2 = p.getErrorStream();
		BufferedReader br1 = new BufferedReader(new InputStreamReader(is1));
		BufferedReader br2 = new BufferedReader(new InputStreamReader(is2));
		String b1, b2;
		StringBuffer sb1 = new StringBuffer();
		StringBuffer sb2 = new StringBuffer();
		while ((b1 = br1.readLine()) != null)
			sb1.append(b1 + "\r\n");
		while ((b2 = br2.readLine()) != null)
			sb2.append(b2 + "\r\n");
		br1.close();
		br2.close();
		b1 = ("Process inputStream message:\r\n" + sb1.toString()).trim();
		b2 = ("Process errorStream message:\r\n" + sb2.toString()).trim();
		if (!sb1.toString().equals("")) {
			System.err.println("Process inputStream message:\r\n" + sb1.toString().trim());
			writeToFile(getDesktop() + "\\InputStream.txt", b1);
		}
		if (!sb2.toString().equals("")) {
			System.err.println("Process errorStream message:\r\n" + sb2.toString().trim());
			writeToFile(getDesktop() + "\\ErrorStream.txt", b2);
		}
	}

	private static String getSystemEncodingCharset() {
		return System.getProperty("file.encoding");
	}

	private static String convertCharset(String str, String from, String to) throws IOException {
		return new String(str.getBytes(from), to);
	}

	private static void writeToFile(String fileName, String str) throws IOException {
		File f = new File(fileName);
		System.err.println("writeToFilepath:" + fileName);
		BufferedWriter bw = new BufferedWriter(new FileWriter(f));
		bw.write(str);
		bw.close();
	}

	private static String getDesktop() {
		return System.getenv("USERPROFILE") + "\\desktop";
	}

	private static String getCurrentFolder() {
		return System.getProperty("user.dir");
	}

	public static void openCurrentFolder() throws IOException {
		String aa = new File(new File("a").getAbsolutePath()).getParent();
		aa = System.getProperty("user.dir");
		open(aa);
	}

	public static void open(String string) throws IOException {
		java.awt.Desktop.getDesktop().open(new File(string));
	}

}
